﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Freedom.Models.Interfaces;
using Freedom.Models.Entities;

namespace Freedom.Models.Services
{
    /// <summary>
    /// 成员信息服务
    /// autor: Penn Yao
    /// </summary>
    public class MemberInfoService : IMemberInfoService
    {
        db_FreedomEntities db;

        public MemberInfoService()
        {
            if(db == null)
                db = new db_FreedomEntities();
        }

        /// <summary>
        /// 添加对象
        /// </summary>
        /// <param name="info">对象</param>
        void IMemberInfoService.addInfo(tb_MemberInfo info)
        {
            db.AddTotb_MemberInfo(info);
            db.SaveChanges();
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="nID">ID</param>
        /// <returns></returns>
        bool IMemberInfoService.deleteInfo(int nID)
        {
            var info = db.tb_MemberInfo.FirstOrDefault(p => p.ID == nID);

            if (info != null)
            {
                db.DeleteObject(info);
                db.SaveChanges();

                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新对象
        /// </summary>
        /// <param name="info">对象</param>
        /// <returns></returns>
        void IMemberInfoService.updateInfo(tb_MemberInfo info)
        {
            IQueryable<tb_MemberInfo> allInfo = db.tb_MemberInfo;
            foreach (tb_MemberInfo item in allInfo)
            {
                if (item.ID.Equals(info.ID))
                {
                    item.Photo = info.Photo;
                    item.ChineseName = info.ChineseName;
                    item.EnglishName = info.EnglishName;
                    item.Birthday = info.Birthday;
                    item.Email = info.Email;
                    item.PhoneNumber = info.PhoneNumber;
                    item.TelephoneNumber = info.TelephoneNumber;
                    item.Blog = info.Blog;
                    item.Weibo = info.Weibo;
                    item.Job = info.Job;
                    item.Skill = info.Skill;
                    item.CreateTime = info.CreateTime;
                    item.UpdateTime = info.UpdateTime;
                    item.Remark = info.Remark;
                }
            }
            db.SaveChanges();
        }

        /// <summary>
        /// 获取对象
        /// </summary>
        /// <param name="nID">ID</param>
        /// <returns></returns>
        tb_MemberInfo IMemberInfoService.getInfo(int nID)
        {
            return db.tb_MemberInfo.Where(p => p.ID == nID).FirstOrDefault();
        }

        /// <summary>
        /// 获取所有对象
        /// </summary>
        /// <returns></returns>
        IQueryable<tb_MemberInfo> IMemberInfoService.getAllInfo()
        {
            return db.tb_MemberInfo;
        }

        /// <summary>
        /// 保存
        /// </summary>
        void IMemberInfoService.save()
        {
            db.SaveChanges();
        }
    }
}